package middle;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import util.PrintUtils;

public class Solution_284 {

    public static void main(String[] args) {
        PeekingIterator iterator = new PeekingIterator(PrintUtils.newArrayList(1, 2, 3).iterator());
        System.out.println(iterator.next());
        System.out.println(iterator.peek());
        System.out.println(iterator.next());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        System.out.println(iterator.hasNext());
    }

    static class PeekingIterator implements Iterator<Integer> {

        private Queue<Integer> queue;

        public PeekingIterator(Iterator<Integer> iterator) {
            // initialize any member here.
            queue = new LinkedList<>();
            while (iterator.hasNext()) {
                queue.add(iterator.next());
            }
        }

        // Returns the next element in the iteration without advancing the iterator.
        public Integer peek() {
            return queue.peek();
        }

        // hasNext() and next() should behave the same as in the Iterator interface.
        // Override them if needed.
        @Override
        public Integer next() {
            return queue.poll();
        }

        @Override
        public boolean hasNext() {
            return !queue.isEmpty();
        }
    }
}
